package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.ControlDataColorTemperatureBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.utils.LinkageDataHolder;
import com.qiantu.phone.widget.StatusSeekBar;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class ColorTemperatureSettingActivity extends AppActivity {
    private SettingBar settingAction;
    private TextView tvCondition;
    private RadioGroup whenRadioGroup;
    private StatusSeekBar statusSeekBar;
    //1小于、2等于、3大于
    private int logicType = 1;
    private String logicTypeStr;
    private int mValue = 5000;
    private String unit = "k";
    private String deviceSerialNo;
    private String serialNo;
    private String areaName;
    private String areaCoding;
    private String deviceName;
    private String deviceImage;
    private LinkageBean mLinkageBean;
    private LinkageBean.LinkageCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_color_temperature_setting;
    }

    @Override
    protected void initView() {
        settingAction = findViewById(R.id.setting_action);
        tvCondition = findViewById(R.id.tv_condition);
        whenRadioGroup = findViewById(R.id.when_radio_group);
        statusSeekBar = findViewById(R.id.status_seek_bar);
        setOnClickListener(settingAction);
        whenRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.type_less_than:
                        logicType = 1;
                        logicTypeStr = getString(R.string.less_than);
                        break;
                    case R.id.type_equal_to:
                        logicType = 2;
                        logicTypeStr = getString(R.string.equal_to);
                        break;
                    case R.id.type_greater_than:
                        logicType = 3;
                        logicTypeStr = getString(R.string.greater_than);
                        break;
                }
                tvCondition.setText(getString(R.string.effective_when_time_holder,  logicTypeStr + mValue + unit));
            }
        });
        ((RadioButton)whenRadioGroup.getChildAt(0)).setChecked(true);
        statusSeekBar.setOnSeekBarChangeListener(new StatusSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int value) {
                mValue = value;
                tvCondition.setText(getString(R.string.effective_when_time_holder,  logicTypeStr + mValue + unit));
            }

            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onStopTrackingTouch(int value) {
                mValue = value;
                tvCondition.setText(getString(R.string.effective_when_time_holder,  logicTypeStr + mValue + unit));
            }
        });
        statusSeekBar.setValue(mValue);
        String deviceType = getString("device_type");
        ControlDataColorTemperatureBean colorTemperatureInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataColorTemperatureBean.class);
        statusSeekBar.setMinMaxValue(colorTemperatureInfo.getControlData().getColourtemperature_min(), colorTemperatureInfo.getControlData().getColourtemperature_max());
    }

    @Override
    protected void initData() {
        serialNo = getString("serial_no");
        deviceSerialNo = getString("device_serial_no");
        areaCoding = getString("room_serial_no");
        areaName = getString("floor_room_name");
        deviceName = getString("device_name");
        deviceImage = getString("device_image");
        mLinkageBean = LinkageDataHolder.getLinkageData();
        if (mLinkageBean != null) {
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions != null && linkageConditions.size() > 0 && !TextUtils.isEmpty(serialNo)) {
                for (LinkageBean.LinkageCondition item : linkageConditions) {
                    if (item.getConditionSerialNo().equals(serialNo)) {
                        mLinkageCondition = item;
                        deviceSerialNo = mLinkageCondition.getDeviceSerialNo();
                        deviceName = LLDeviceDBManager.getInstance(ColorTemperatureSettingActivity.this).get(deviceSerialNo).getName();
                        areaName = mLinkageCondition.getAreaName();
                        areaCoding = mLinkageCondition.getAreaCoding();
                        //已设置过的色温条件
                        if(LLDeviceUtil.KELVIN.equals(mLinkageCondition.getStateName())){
                            logicType = mLinkageCondition.getLogicType();
                            try {
                                mValue = Integer.valueOf(mLinkageCondition.getStateValue());
                            }catch (Exception e){

                            }
                        }
                        break;
                    }
                }
            }
        }
        updateView();
    }

    private void updateView() {
        switch (logicType) {
            case 1:
                ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                break;
            case 2:
                ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                break;
            case 3:
                ((RadioButton) whenRadioGroup.getChildAt(4)).setChecked(true);
                break;
        }
        statusSeekBar.setValue(mValue);
    }

    @Override
    public void onRightClick(View view) {
        //当前联动空，才去
        if (mLinkageCondition == null) {
            //本地保存联动条件
            if (mLinkageBean == null) {
                mLinkageBean = new LinkageBean();
                LinkageDataHolder.setLinkageData(mLinkageBean);
            }
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions == null) {
                linkageConditions = new ArrayList<>();
            }
            mLinkageBean.setLinkageConditions(linkageConditions);
            mLinkageCondition = new LinkageBean.LinkageCondition();
            //先设置个本地的SerialNo，为了编辑找到对应的数据
            mLinkageCondition.setConditionSerialNo(UUID.randomUUID().toString());
            mLinkageCondition.setConditionType(8);
            linkageConditions.add(mLinkageCondition);
            mLinkageCondition.setSort(linkageConditions.indexOf(mLinkageCondition) + 1);
        }
        mLinkageCondition.setAreaType(1);
        mLinkageCondition.setDeviceSerialNo(deviceSerialNo);
        mLinkageCondition.setAreaName(areaName);
        mLinkageCondition.setAreaCoding(areaCoding);
        mLinkageCondition.setBeginValue(mValue);
        mLinkageCondition.setEndValue(mValue);
        mLinkageCondition.setLogicType(logicType);
        mLinkageCondition.setImageUrl(deviceImage);
        mLinkageCondition.setBriefing(deviceName);
        mLinkageCondition.setStateName(LLDeviceUtil.KELVIN);
        mLinkageCondition.setStateValue(mValue + "");
        startActivity_SingleTask(EditLinkageActivity.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.setting_action:
                showBottomListDialog();
                break;
        }
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            mBottomActionDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add("变为");
            data.add("处于");
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    if(position == 0){
                        settingAction.setRightText("变为");
                    }else{
                        settingAction.setRightText("处于");
                    }
                    mBottomActionDialog.dismiss();
                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }
}